﻿using System;
using System.Collections.Generic;
using System.Linq;
using YJKRevitBase.Algorithm;
using YJKRevitBase.Math;
using YJKRevitKernel.RevitTopologySuite;
using YJKRevitDimensionText.VerticalAutoDim.ProgressBar;

namespace YJKRevitDimensionText.VerticalAutoDim.GeoAlgorithm
{
	internal class GeoDepthClippedAlgorithm : IGeoDepthClippedAlgorithm
	{
		public IList<GeoAABB> ArrayGeoSubjectAABB
		{
			get
			{
				return this._arraySubjectAABB;
			}
		}

		public GeoDepthClippedAlgorithm(IList<GeoAABB> arraySubjectAABB, IDualStatusProgressBarProcess iDualStatusProgressBarProcess)
		{
			this.DualStatusProgressBarProcess = iDualStatusProgressBarProcess;
			this._arraySubjectAABB = arraySubjectAABB;
		}

		public IList<GeoAABB> GeoDepthClipped(IList<GeoAABB> lstGeoObjectAABBNode)
		{
			List<GeoAABB> list = new List<GeoAABB>();
			Dictionary<GeoAABB, List<GeoAABB>> dictionary = TreeSearchUtility.NearSearch<GeoAABB>(new List<GeoAABB>(this.ArrayGeoSubjectAABB));
			int count = lstGeoObjectAABBNode.Count;
			this.DualStatusProgressBarProcess.UpdatePrimaryStatus(2);
			this.DualStatusProgressBarProcess.SetSubStatusMaxVal(count);
			for (int i = 0; i < count; i++)
			{
				this.DualStatusProgressBarProcess.UpdateSubStatus(i);
				GeoAABB geoAABB = lstGeoObjectAABBNode[i];
				List<GeoAABB> list2 = (from w in dictionary[geoAABB]
				orderby w.IPlanarGeo.Distance2Plane
				select w).ToList<GeoAABB>();
				for (int j = 0; j < list2.Count; j++)
				{
					GeoAABB geoAABB2 = list2[j];
					if (!geoAABB.Equals(geoAABB2))
					{
						if (MathUtility.LessThan(geoAABB.IPlanarGeo.Distance2Plane, geoAABB2.IPlanarGeo.Distance2Plane))
						{
							break;
						}
                        geoAABB.Intersect(geoAABB2, (YJKRevitBase.TopologySuite.BooleanOperatorKindEnum)2);
					}
				}
				double dArea = 0.0;
				EnumerableExternsion.ForEach<RevitPolygonDataBean>(geoAABB.ArrayBoundaryPolygonClippedResult, delegate(RevitPolygonDataBean p)
				{
					if (p.Area != null)
					{
						dArea += p.Area.Value;
					}
				});
				if (MathUtility.GreaterThan(dArea, 1.0))
				{
					list.Add(geoAABB);
				}
			}
			return list;
		}

		private readonly IList<GeoAABB> _arraySubjectAABB;

		private readonly IDualStatusProgressBarProcess DualStatusProgressBarProcess;
	}
}
